<template>
  <div>
    <el-dialog
      :title="isEdit?'添加部门':'编辑部门'"
      :visible="dialogVisible"
      width="50%"
      :before-close="handleClose"
      @close="dialogCloseFn"

      @update:visible="val=>$emit('showDialog',false)"
    >
      <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
            <el-option v-for="obj in EmployeesSimpleList" :key="obj.id" :value="obj.username" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    dialogVisible: {
      type: Boolean
    },
    EmployeesSimpleList: {
      type: Array,
      default: () => []

    },
    originList: {
      type: Array,
      default: () => []
    },
    departmentsId: {
      type: String,
      default: ''
    },
    isEdit: {
      type: Boolean,
      default: true
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      let codeList = this.originList.map(item => item.code)
      if (!this.isEdit) {
        codeList = this.originList.filter(item => item.id !== this.departmentsId).map(item => item.code)
      }

      codeList.includes(value) ? callback(new Error(`编码${value}已经存在`)) : callback()
    }
    const validName = (rule, value, callback) => {
      let nameList = this.originList.filter(item => item.pid === this.departmentsId).map(item => item.name)
      if (!this.isEdit) {
        const obj = this.originList.find(item => item.id === this.departmentsId)
        const pid = obj.id
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.departmentsId).map(item => item.name)
      }
      nameList.includes(value) ? callback(new Error(`部门名称${value}已存在`)) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 弹窗关闭前的回调
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 取消按钮
    cancelFn() {
      this.$emit('showDialog', false)
    },
    // 确认按钮
    confirmFn() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          this.$emit('addDepartEV', this.form)
          this.$emit('showDialog', false)
        }
      })
    },
    // 弹窗关闭后的回调
    dialogCloseFn() {
      this.$refs.deptForm.resetFields()
    }
  }
}
</script>

<style scoped>

</style>
